package cn.iocoder.yudao.module.system.util.a_gaojian;

import cn.iocoder.yudao.module.system.controller.admin.a_pictures.vo.PicturesReqVO;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lxj
 */
public class ManuscriptLogUtils {

    //将对象转为map循环map后对比
    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            map.put(fieldName, value);
        }
        return map;
    }

    public static Map<String, Object> mapCompare(Map<String, Object> map1, Map<String, Object> map2) {
        for (String key : map1.keySet()) {
            if (map1.get(key) != null) {
                String value = map1.get(key).toString();
                if (map2.get(key) == null) {
                    map2.put(key, value);
                } else {
                    if (!value.equals(map2.get(key))) {
                        map2.put(key, value);
                    }
                }
            }
        }
        return map2;
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map.put("content", "张三");
        map.put("author", 25);
        map.put("content1", null);

        map2.put("author", 25);
        map2.put("content", "李四");

        for (String key : map.keySet()) {
            if (map.get(key) != null) {
                String value = map.get(key).toString();
                if (map2.get(key) == null) {
                    map2.put(key, value);
                } else {
                    if (!value.equals(map2.get(key))) {
                        map2.put(key, value);
                    }
                }
            }
        }
        System.out.println(map);
        System.out.println(map2);

        ObjectMapper objectMapper = new ObjectMapper();
        PicturesReqVO user = objectMapper.convertValue(map, PicturesReqVO.class);
        System.out.println(user);
    }

    public String ueditorConfig() {
        File file = new File("D:\\idea\\jfj\\yudao-module-system\\yudao-module-system-biz\\src\\main\\resources\\config.json"); // 指定要读取的文件路径
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        assert resourceAsStream != null;
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAsStream))) {
            String line;
            final StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
